package com.example.demolearn_1.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.demolearn_1.MainActivity;
import com.example.demolearn_1.R;

import java.util.ArrayList;

public class GuideActivity extends Activity {

    private ViewPager mVp_guide;
    private Button mBtn_start;
    private LinearLayout mLl_point;
    private ArrayList<View> pageView;
    private ImageView[] imageViews;
    private Context mContext = GuideActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        mVp_guide = (ViewPager) findViewById(R.id.vp_guide);
        mBtn_start = (Button) findViewById(R.id.btn_start);
        mLl_point = (LinearLayout) findViewById(R.id.ll_point);

        LayoutInflater inflater =getLayoutInflater();
        View view1 = inflater.inflate(R.layout.guide_item01, null);
        View view2 = inflater.inflate(R.layout.guide_item02, null);
        View view3 = inflater.inflate(R.layout.guide_item03, null);

        pageView = new ArrayList<View>();
        pageView.add(view1);
        pageView.add(view2);
        pageView.add(view3);

        imageViews = new ImageView[pageView.size()];
        for (int i = 0; i < pageView.size(); i++) {
            ImageView imageView = new ImageView(GuideActivity.this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);;
            layoutParams.setMargins(20,0,0,0);
            imageView.setLayoutParams(layoutParams);

            if ( i  ==  0 ){
                imageView.setBackgroundResource(R.drawable.guide_point_focused);
            }else {
                imageView.setBackgroundResource(R.drawable.guide_point_unfocused);
            }

            imageViews[i] = imageView;
            mLl_point.addView(imageViews[i]);
        }

        mVp_guide.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return pageView.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(pageView.get(position));
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(pageView.get(position));
                return pageView.get(position);
            }
        });

        mVp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                for (int j = 0; j < imageViews.length; j++) {
                    if( j  == i){
                        imageViews[i].setBackgroundResource(R.drawable.guide_point_focused);
                    }else{
                        imageViews[j].setBackgroundResource(R.drawable.guide_point_unfocused);
                    }
                }

                if( i  == imageViews.length - 1 ){
                    mBtn_start.setVisibility(View.VISIBLE);
                }else {
                    mBtn_start.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        mBtn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext, MainActivity.class));
                finish();
            }
        });
    }
}
